﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NUS.ISS.ARS.Web.PageController;
using NUS.ISS.ARS.BusinessEntity.ViewModels;
using System.Text;

namespace NUS.ISS.ARS.Web.Booking
{
    public partial class Summary : BookingBaseController
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CurrentPage = "Summary.aspx";
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                btnPre.Visible = IsPrePageVisible;
                btnNext.Visible = IsNextPageVisible;
            }
            ShowSummary();
        }

        private void ShowSummary()
        {
            if (FlightBookingTrips != null && FlightBookingTrips.Count() > 0)
            {
                ShowPriceSummary();
                ShowBookingSummary();
            }
        }       

        private void ShowPriceSummary()
        {
            if (IsManageBooking)
            {
                ltPriceSummaryTitle.Text = "<div><b>Price Diffenence Details</b></div>";
            }
            else
            {
                ltPriceSummaryTitle.Text = "<div><b>Price Details</b></div>";
            }

            ltPriceSummary.Text = PreparePriceSummaryHtml(GetPriceSummary());
        }

        private void ShowBookingSummary()
        {
            if (IsManageBooking)
            {
                ltBookingSummaryTitle.Text = "<div><b>Changed Booking Summary</b></div>";
                lvBookingSummary.DataSource = FormatFlightBookingTripViewForDisplay(FlightBookingTrips.Where(t => t.ToBeChanged != true)); //still keeps old trips
            }
            else
            {
                ltBookingSummaryTitle.Text = "<div><b>Booking Summary</b></div>";
                lvBookingSummary.DataSource = FormatFlightBookingTripViewForDisplay(ReservationData.SelectedBookingTrips); //only selected trips
            }
            
            lvBookingSummary.DataBind();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            MoveToNextPage();
        }
        protected void btnPre_Click(object sender, EventArgs e)
        {
            MoveToPrePage();
        }

        private string PreparePriceSummaryHtml(PriceSummaryView ps)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<table>");
            if (ReservationData.NumOfAdult == 1)
            {
                sb.Append("<tr><td>" + ReservationData.NumOfAdult + " Adult: $ </td><td>" + ps.AdultPrice + "</td></tr>");
            }
            else
            {
                sb.Append("<tr><td>" + ReservationData.NumOfAdult + " Adults: $ </td><td>" + ps.AdultPrice + "</td></tr>");
            }

            if (ps.isIncludeChild)
            {
                if (ReservationData.NumOfChild == 1)
                {
                    sb.Append("<tr><td>" + ReservationData.NumOfChild + " Child: $ </td><td>" + ps.ChildPrice + "</td></tr>");
                }
                else
                {
                    sb.Append("<tr><td>" + ReservationData.NumOfChild + " Children: $ </td><td>" + ps.ChildPrice + "</td></tr>");
                }
            }
            sb.Append("<tr><td> Surcharge: $ </td><td>" + ps.Surcharge + "</td></tr>");
            sb.Append("<tr><td> Total Due: $ </td><td>" + ps.TotalDue + "</td></tr>");
            sb.Append("</table> <br />");

            return sb.ToString();
        }


        protected List<object> FormatFlightBookingTripViewForDisplay(IEnumerable<FlightBookingTripView> tripViews)
        {
            List<object> trips = new List<object>();
            string format = @"$ {0} <br /> {1} ({2}) - {3} ({4})<br /> {5} <hr> Surcharge: $ {6}";
            foreach (FlightBookingTripView t in tripViews)
            {
                trips.Add(new
                {
                    id = t.FlightBookingTripId,
                    desc = string.Format(format, t.TotalPrice, t.DepartureTime.ToString(@"hh\:mm"), t.DepartureCityCode, t.ArrivalTime.ToString(@"hh\:mm"), t.ArrivalCityCode, t.FlightDate.ToLongDateString(), t.Surcharge == null ? (decimal)0.00M : t.Surcharge)
                });
            }

            return trips;
        }
    }
}